package com.bdqn.hospital.controller;

import com.bdqn.hospital.pojo.SysUsers;
import com.bdqn.hospital.service.SysUsersService;
import com.bdqn.hospital.service.impl.SysUsersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.bdqn.hospital.pojo.Department;
import com.bdqn.hospital.pojo.SysUsers;
import com.bdqn.hospital.service.DepartmentService;
import com.bdqn.hospital.service.SysUsersService;
import com.bdqn.hospital.service.impl.DepartmentServiceImpl;
import com.bdqn.hospital.service.impl.SysUsersServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @version : 1.0
 * @Author :guogd
 * @createTime: 2023/03/01 14:40
 * @Description: com.bdqn.hospital.controller
 */
@Controller
@RequestMapping("sysUser")
public class SysUsersController {
    /**
     * 用户Controller
     */

    @Autowired
    private SysUsersService sysUsersService;

    @RequestMapping("index")
    public String index(){
        return "developerLogin";
    }

    /**
     * 登录
     * @param session
     * @param resp
     * @param req
     * @param userName
     * @param password
     * @throws IOException
     */
    @PostMapping("login")
    public void login(HttpSession session, HttpServletResponse resp, HttpServletRequest req,
                      @RequestParam("account") String userName, @RequestParam("password") String password) throws IOException {
        String html = "";
        resp.setContentType("text/html;charset=utf-8");
        SysUsers user = sysUsersService.logIn(userName, password);
        if(user!=null){
            session.setAttribute("user",user);
            switch (user.getUserType()){
                case 1: //1-->系统管理员；
                    //html = "redirect:getUserAll";
                    html = "/sysUser/getUserAll";
                    break;
                case 2: //2-->挂号员；
                    html = "/registry/toRegistry";
                    break;
                case 3://3-->门诊医师;
                    html = "/registry/getAll";
                    break;
                case 4://4-->收银员；
                    html = "/charge/goToPay";
                    break;
                case 5: //5-->药方医师
                    html = "/prescription/toDeveloperAppinfomodify";
                    break;
                //default:
                //    html = "develpoerMain";
                //    break;

            }
            resp.getWriter().print("<script>alert('登陆成功');location.href='"+html+"'</script>");
        }else{
            resp.getWriter().print("<script>alert('登陆失败');history.back();</script>");
        }

    }

    @RequestMapping("exit")
    public String exit(HttpSession session){
        session.removeAttribute("user");
        return "developerLogin";
    }

    //科室Service
    @Autowired
    private DepartmentServiceImpl departmentService;


/**
     * 获取所有用户信息
     */

    @RequestMapping("getUserAll")
    private String getUserAll(Model model, @RequestParam(defaultValue = "1",required = false)Integer curPage){
        List<SysUsers> list = sysUsersService.getAll();

/*分页*/

        PageHelper.startPage(curPage,10);
        PageInfo<SysUsers> page = new PageInfo<>(list);
        model.addAttribute("userList",page);
        return "houApplist";
    }


/*获取自定义文件路径*/

    @Value("${file666}")
    private String filepath;

/**
     * 添加用户
     */

    @RequestMapping("insertUser")
    private String insertUser(SysUsers user,  MultipartFile file, HttpSession session) throws IOException {
        //获取user中的用户 取出Id
        SysUsers loginInUser = (SysUsers) session.getAttribute("user");
        String fileName = "";
        //用户上传了头像
        if(!file.isEmpty()){
            File f = new File(filepath);
            if(!f.exists()){
                //若该地址没有文件则创建
                f.mkdir();
            }
            //获取文件后缀
            String houzhui = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            //重置文件名
             fileName = UUID.randomUUID().toString().replace("-","").toUpperCase()+houzhui;
            f = new File(filepath,fileName);
            file.transferTo(f);
        }
       user.setCreatedUserId(loginInUser.getCreatedUserId());

        user.setHeadUrl(fileName);
        System.out.println(user);
        Integer i = sysUsersService.addUsers(user);
        System.out.println(i);
        return "redirect:getUserAll";
    }


/**
     * 传入新增用户页面
     */

    @RequestMapping("goAddUser")
    public String goAddUser(Model model){
        List<Department> departmentAll = departmentService.getDepartmentAll();
        model.addAttribute("departmentList",departmentAll);
        return "houAppcheck";
    }


}
